package local.app;


import com.jcraft.jsch.JSchException;
import local.app.server_audits.MultiChecker;
import local.app.servers.*;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

public class App {

    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, JSchException {

        ArrayList<Server> servers = new ArrayList<>();
        String jarPath = App.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        String file = new File(jarPath).getParent() + File.separator + "list_ip";

        try (
                BufferedReader fileReader = new BufferedReader(new FileReader(file));
                BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file + "_result", true))
        ) {

            String line;
            while ((line = fileReader.readLine()) != null) {
                String[] nameIp = line.split("(\\s+)(?=192)");
                switch (nameIp[0]) {
//                    case "ОТУ":
//                        servers.add(new OtuServer(nameIp[0], nameIp[1]));
//                        break;
//                    case "РЦОД":
//                        servers.add(new RCodServer(nameIp[0], nameIp[1]));
//                        break;
                    case "Криптосервер ВСХ":
                        servers.add(new CryptoVsxServer(nameIp[0], nameIp[1]));
                        break;
                    case "Криптосервер":
                        servers.add(new CryptoServer(nameIp[0], nameIp[1]));
                        break;
                    default:
                        servers.add(new StandardServer(nameIp[0], nameIp[1]));
                }

            }

            fileWriter.write("--------------------------------------");
            CountDownLatch latch = new CountDownLatch(servers.size());
            for (Server server :
                    servers) {
                new MultiChecker(server, fileWriter, latch).start();
            }
            latch.await();

            System.out.println("Все потоки завершены!\nНажмите CTRL + C для выхода...");

        }
    }
}
